package com.link.base.base.performance.controller;

import com.link.base.base.performance.model.ProjectBonus;
import com.link.base.base.performance.service.ProjectBonusService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import com.link.core.cllog.LCLogger;
import com.link.core.modules.export.ExportFactory;
import com.link.core.modules.export.ExportWriter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目奖金
 *
 * @author 王昊
 * @date 2021-04-08 13:17:18
 */
@Controller
@RequestMapping("/link/projectBonus")
public class ProjectBonusController extends BasicController<ProjectBonus> {

    private static final Logger logger = LogManager.getLogger(ProjectBonusController.class);

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private ProjectBonusService projectBonusService;

    @Override
    public BasicService<ProjectBonus> getBasicService() throws Exception {
        return projectBonusService;
    }

    /**
     *计算项目奖金所有字段
     *
     * @author 王昊
     * @date 2021/04/12
     */
    @RequestMapping(value = "/calculateBonus", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> calculateBonus(@JsonParam ProjectBonus entity, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            projectBonusService.calculateBonus(entity);
            result.put("newRow",entity);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 年度奖金明细表 报表
     * @author 王昊
     * @date 2021/05/06
     */
    @RequestMapping(value = "/annualBonusReport", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> annualBonusReport(@JsonParam QueryParams queryParams, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            ProjectBonus entity = BasicModel.transformClass(ProjectBonus.class,queryParams);
            List<ProjectBonus> list = projectBonusService.annualBonusReport(entity);
            result.put("rows",list);
            result.put("success", true);
            result.put("total", getCount(entity, list.size()));
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     *年度奖金明细报表 导出报表
     * @param qps
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/annualBonusReport/exportData")
    @ResponseBody
    public Map<String, Object> projectUndertakingReport(@JsonParam QueryParams qps, HttpSession session,
                                                        HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<ProjectBonus> list = new ArrayList<>();
        try {
            ExportWriter exportWriter;
            String exportType = "xlsx";
            String exportFileType = qps.getExportFileType();
            //区分导出文件格式xlsx||Excel
            if (exportType.equals(exportFileType)) {
                exportWriter = ExportFactory.newExcelInstance(qps);
            } else {
                exportWriter = ExportFactory.newInstance(qps);
            }
            ProjectBonus entity = BasicModel.transformClass(ProjectBonus.class,qps);
            while (!qps.getStopExport()) {
                list = projectBonusService.annualBonusReport(entity);
                exportWriter.wirteData(list);
                //导出当前页
                if ("Y".equals(qps.getExportCurrentPage())) {
                    break;
                }
            }

            result.put("success", true);
            result.put("url", exportWriter.getUrlPath());
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping({"/save"})
    @ResponseBody
    public Map<String, Object> save(@JsonParam ProjectBonus entity, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap(4);

        try {
            this.beforInsert(entity, request);
            this.beforUpsert(entity, request);
            projectBonusService.save(entity);
            ProjectBonus newEntry = this.getBasicService().queryById(entity);
            newEntry.setAllTotalBonus(entity.getAllTotalBonus());
            result.put("newRow", newEntry);
            result.put("success", true);
            result.put("code", "200");
        } catch (BasicServiceException var7) {
            logger.error("BasicController捕获异常: ", var7);
            result.put("success", false);
            result.put("result", var7.getMessage());
            result.put("detailMessage", var7.getDetailMessage());
            result.put("code", var7.getCode());
        } catch (Exception var8) {
            logger.error("BasicController捕获异常: ", var8);
            result.put("success", false);
            result.put("result", var8.getMessage());
            result.put("code", "400");
        }

        return result;
    }

    @RequestMapping({"/renew"})
    @ResponseBody
    public Map<String, Object> renew(@JsonParam ProjectBonus entity, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap(4);

        try {
            this.beforUpdate(entity, request);
            this.beforUpsert(entity, request);
            this.checkData(entity, request);
            if (!this.checkExistDataCache("base", session, entity)) {
                throw new BasicServiceException("非法更新数据！");
            }
            projectBonusService.renew(entity);
            ProjectBonus newEntry = this.getBasicService().queryById(entity);
            newEntry.setAllTotalBonus(entity.getAllTotalBonus());
            result.put("newRow", newEntry);
            result.put("success", true);
            result.put("code", "200");
        } catch (BasicServiceException var7) {
            logger.error("BasicController捕获异常: ", var7);
            result.put("success", false);
            result.put("result", var7.getMessage());
            result.put("detailMessage", var7.getDetailMessage());
            result.put("code", var7.getCode());
        } catch (Exception var8) {
            logger.error("BasicController捕获异常: ", var8);
            result.put("success", false);
            result.put("result", var8.getMessage());
            result.put("code", "400");
        }

        return result;
    }

}
